import { createSlice } from '@reduxjs/toolkit';

const initialStateAccount = {
  balance: 0,
  loan: 0,
  loanPurpose: '',
  isLoading: false,
};

const accountSlice = createSlice({
  name: 'account',
  initialState: initialStateAccount,
  reducers: {
    deposit: (state, action) => {
      state.balance += action.payload;
    },
    withdraw: (state, action) => {
      state.balance -= action.payload;
    },
    loan: (state, action) => {
      if (state.loan > 0) return;
      state.loan = action.payload.amount;
      state.loanPurpose = action.payload.purpose;
    },
    payLoan: (state) => {
      state.loan = 0;
      state.balance -= state.loan;
      state.loanPurpose = '';
    },
  },
});

export const { withdraw, loan, payLoan } = accountSlice.actions;

export default accountSlice.reducer;

export function deposit(amount, currency) {
  if (currency === 'USD') {
    return {
      type: 'account/deposit',
      payload: amount,
    };
  }

  return async function (dispatch, getState) {
    dispatch({ type: 'account/convertingCurrency' });
    const res = await fetch(
      `https://api.frankfurter.app/latest?amount=${amount}&from=${currency}&to=USD`,
    );
    const data = await res.json();
    const converted = data.rates.USD;

    dispatch({
      type: 'account/deposit',
      payload: converted,
    });
  };
}

// function accountReducer(state = initialStateAccount, action) {
//   switch (action.type) {
//     case 'account/convertingCurrency':
//       return {
//         ...state,
//         isLoading: true,
//       };
//     case 'account/deposit':
//       return {
//         ...state,
//         isLoading: false,
//         balance: state.balance + action.payload,
//       };
//     case 'account/withdraw':
//       return {
//         ...state,
//         balance: state.balance - action.payload,
//       };
//     case 'account/loan':
//       if (state.loan > 0) return state;
//       return {
//         ...state,
//         loan: action.payload.amount,
//         loanPurpose: action.payload.purpose,
//       };
//     case 'account/payLoan':
//       return {
//         ...state,
//         loan: 0,
//         balance: state.balance - state.load,
//         loanPurpose: '',
//       };
//     default:
//       return state;
//   }
// }

// export function deposit(amount, currency) {
//   if (currency === 'USD') {
//     return {
//       type: 'account/deposit',
//       payload: amount,
//     };
//   }

//   return async function (dispatch, getState) {
//     dispatch({ type: 'account/convertingCurrency' });
//     const res = await fetch(
//       `https://api.frankfurter.app/latest?amount=${amount}&from=${currency}&to=USD`,
//     );
//     const data = await res.json();
//     const converted = data.rates.USD;

//     dispatch({
//       type: 'account/deposit',
//       payload: converted,
//     });
//   };
// }

// export function withdraw(amount) {
//   return {
//     type: 'account/withdraw',
//     payload: amount,
//   };
// }

// export function loan(amount, purpose) {
//   return {
//     type: 'account/loan',
//     payload: {
//       amount,
//       purpose,
//     },
//   };
// }

// export function payLoan() {
//   return {
//     type: 'account/payLoan',
//   };
// }

// export default accountReducer;
